import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common'
import { Observable, map } from 'rxjs'
import { Response } from 'express'

@Injectable()
export class FormatResponseInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const response = context.switchToHttp().getResponse<Response>()
    const code = String(response.statusCode).startsWith('2') ? 200 : response.statusCode

    return next.handle().pipe(map((data) => {
      return {
        code,
        message: 'success',
        data,
      }
    }))
  }
}
